home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / M / May MacUser Program.cpt / miniGenApp Src / Shell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-29  |  7.1 KB  |  307 lines  |  [TEXT/KAHL]

  1. /* *****************************************************************************
  2.     FILE:             Shell.c
  3.     
  4.     DESCRIPTION:     Main event loop and event handling utilities
  5.  
  6.     AUTHOR:            Kurt W.G. Matthies
  7.         
  8.     Copyright © 1990 by Code of the West, Inc. All Rights Reserved.
  9.     
  10.     Revision History:
  11.     ==========================================================
  12.     3.30.90    -    May 1990 MacUser Release
  13.     ==========================================================
  14.  
  15.    ***************************************************************************** */
  16. #define _Main_Module_
  17.  
  18. #include "AppGlobals.h"
  19.  
  20. #ifdef V2
  21. #include <ControlMgr.h>
  22. #include <DialogMgr.h>
  23. #include <EventMgr.h>
  24. #endif
  25.  
  26. #include "AppInitPr.h"
  27. #include "DisplayPr.h"
  28. #include "DocUtilPr.h"
  29. #include "MenuUtilPr.h"
  30.  
  31. #include "ShellPr.h"
  32.     
  33.     
  34. /* --------------------  Local Prototypes --------------------------------- */
  35.                         main                    ( void );
  36. void                    doKeyDown                 ( EventRecord *e );
  37. void                    doMouseDown             ( EventRecord *e );
  38. void                    doActEvent                ( EventRecord *e );
  39. void                    doUpdateEvent            ( EventRecord *e );
  40. void                    doSuspendResume            ( EventRecord *e );
  41. void                    doInContent             ( WindowPtr, EventRecord *e );
  42. /* -------------------------------------------------------------------------
  43.     main -    program entry point
  44. ---------------------------------------------------------------------------- */
  45. main ()
  46. {
  47.     EventRecord      event;
  48.     short             dItem;
  49.     DialogPtr        dialogPtr;
  50.     Boolean            result;
  51.     long            waitTicks;
  52.  
  53.     initApplication ();
  54.     
  55.     openAppDocs ();
  56.  
  57.     FlushEvents (everyEvent, 0);    /* dump all pending events */
  58.     
  59.     waitTicks = 20;        /* the Multifinder share time value */
  60.     
  61.     while (true)    /* main event loop */
  62.     {    
  63.         if (gHasWNE)
  64.             result = WaitNextEvent (everyEvent, &event, waitTicks, 0L);
  65.         else
  66.         {
  67.             SystemTask ();
  68.             result = GetNextEvent (everyEvent, &event);
  69.         }
  70.         
  71.         if (result)        /* if there is an event */
  72.         {
  73.             fixMenus ();            /* hilite/unhilite appropriate menu items */
  74.             
  75.             switch (event.what) 
  76.             {
  77.                 case mouseDown:    
  78.                     doMouseDown (&event);
  79.                     break;
  80.             
  81.                 case keyDown:
  82.                     doKeyDown (&event);
  83.                     break;
  84.             
  85.                 case activateEvt:    
  86.                     doActEvent (&event);
  87.                     break;
  88.     
  89.                 case updateEvt:
  90.                     doUpdateEvent (&event);
  91.                     break;
  92.                     
  93.                 case app4Evt:
  94.                     doSuspendResume (&event);
  95.                     break;
  96.                     
  97.                 default:        /* 
  98.                                     you may want to put some test code here
  99.                                     to see what kind of events an application
  100.                                     can receive
  101.                                 */
  102.                     break;
  103.                     
  104.             }
  105.         }
  106.     }    /* main event loop */
  107.  
  108. /*--------------------------------------------------------------------------
  109.     doKeyDown -     react to a key down event
  110.     3.30.90kwgm
  111. ---------------------------------------------------------------------------*/
  112. static void 
  113. doKeyDown (e)
  114.     EventRecord     *e;
  115. {
  116.     char             c;
  117.     
  118.     c = e->message & charCodeMask;        /* get character from event record */
  119.     
  120.     if (e->modifiers & cmdKey)
  121.     {
  122.         doMenu (MenuKey (c));        /* do menu commands */
  123.         HiliteMenu (0);
  124.     }
  125. }
  126.  
  127. /*------------------------------------------------------------------------------
  128.     doMouseDown -  react to a mouse down event
  129.     3.30.90kwgm
  130. --------------------------------------------------------------------------------*/
  131. static void 
  132. doMouseDown (e)
  133.     EventRecord     *e;
  134. {
  135.     WindowPtr         theWindow;
  136.     short              where;
  137.  
  138.     where = FindWindow (e->where, &theWindow);
  139.     
  140.     switch (where) 
  141.     {    
  142.         case inDesk:        /* not interested */
  143.             break;
  144.         
  145.         case inMenuBar:
  146.             doMenu (MenuSelect (e->where));
  147.             break;
  148.         
  149.         case inSysWindow:
  150.             SystemClick (e, theWindow);
  151.             break;
  152.     
  153.         case inContent:
  154.             if (theWindow != FrontWindow() || !((WindowPeek)theWindow)->hilited)
  155.                 SelectWindow (theWindow);
  156.             else
  157.                 doInContent (theWindow, e);
  158.             break;
  159.     
  160.         case inGoAway:
  161.             if (TrackGoAway (theWindow, e->where))
  162.                 doCloseDoc (theWindow);
  163.             break;
  164.     }
  165.  
  166. } /* doMouseDown */
  167.  
  168.  
  169. /*-----------------------------------------------------------------------------
  170.     doActEvent -    react to an activate event
  171.     3.30.90kwgm
  172. ------------------------------------------------------------------------------*/
  173. static void 
  174. doActEvent (e)
  175.     EventRecord     *e;
  176. {
  177.     WindowPtr         theWindow;          /* the active window */
  178.     ControlHandle    controlList;
  179.     Rect            scrollbarRect;
  180.  
  181.     theWindow = (WindowPtr) e->message;
  182.     SetPort (theWindow);
  183.     
  184.     controlList =  ((WindowPeek)theWindow)->controlList;
  185.     
  186.     HidePen();
  187.     if (e->modifiers & activeFlag)
  188.     {
  189.         /* activate the controls of the newly activated window */
  190.         while (controlList)
  191.         {
  192.             ShowControl (controlList);
  193.             controlList = (*controlList)->nextControl;
  194.         }
  195.     }
  196.     else
  197.     {
  198.         /* deactivate the controls of the newly deactivated window */
  199.         while (controlList)
  200.         {
  201.             HideControl (controlList);
  202.             controlList = (*controlList)->nextControl;
  203.         }
  204.     }
  205.     ShowPen();
  206.  
  207. } /* doActEvent */
  208.  
  209. /* ----------------------------------------------------------------------------
  210.     doUpdateEvent -        react to the update event
  211.     3.30.90kwgm
  212. -------------------------------------------------------------------------------*/
  213. static void 
  214. doUpdateEvent (e)
  215.     EventRecord        *e;
  216. {
  217.     WindowPtr        theWindow;
  218.     GrafPtr            savePort;
  219.     
  220.     theWindow = (WindowPtr) e->message;
  221.  
  222.     if (((WindowPeek) theWindow)->windowKind  >= 1)        /* is my window?  */
  223.     {
  224.         GetPort (&savePort);
  225.         SetPort (theWindow);
  226.         BeginUpdate (theWindow);
  227.         
  228.         EraseRgn ((WindowPeek)theWindow->visRgn);
  229.         
  230.         drawDocContents (theWindow);
  231.         
  232.         EndUpdate (theWindow);
  233.         SetPort (savePort);
  234.     }
  235.     
  236. } /* doUpdateEvent */
  237.  
  238. /* ------------------------------------------------------------------------------
  239.     doInContent -    called from doMouseDown, respond to mouse down in window
  240.     3.30.90kwgm 
  241. --------------------------------------------------------------------------------- */
  242. static void 
  243. doInContent (theWindow, eventPtr)
  244.     WindowPtr                theWindow;
  245.     EventRecord                *eventPtr;
  246. {
  247.     short                    part;
  248.     ControlHandle            theControl;
  249.     Point                    where;
  250.     
  251.     SetPort (theWindow);
  252.     
  253.     where = eventPtr->where;
  254.     
  255.     GlobalToLocal (&where);
  256.         
  257.     /* this is the start of something, but there's nothing to do here this month */
  258.     
  259. }    /* doInContent */
  260.  
  261. /* -----------------------------------------------------------------------------------
  262.     doSuspendResume -    do necessary things on multifinder switch
  263.     3.30.90kwgm
  264. -------------------------------------------------------------------------------------- */
  265. static void 
  266. doSuspendResume (e)
  267.     EventRecord        *e;
  268. {
  269.     Boolean            activate;
  270.     WindowPtr        topWindow;
  271.     
  272.     /* Suspend == false, Resume == true */
  273.     activate = (e->message & 1) ? true : false;
  274.     
  275.     /* if active window, activate/deactivate */
  276.     topWindow = FrontWindow ();
  277.     
  278.     if ((((WindowPeek) topWindow)->windowKind == userKind)) 
  279.     {
  280.         if (activate)
  281.             SelectWindow (topWindow);
  282.         else
  283.             HiliteWindow (topWindow, false);
  284.     }
  285.  
  286. } /* doSuspendResume */
  287.  
  288. /*------------------------------------------------------------------------------
  289.     cleanExit -     do the assorted things which must be done on exit
  290.     3.30.90kwgm
  291. -------------------------------------------------------------------------------- */
  292. void 
  293. cleanExit (normal)
  294.     Boolean        normal;
  295. {
  296.     WindowPeek        theWindow;
  297.     
  298.     if (theWindow = FrontWindow ())
  299.         if (theWindow->windowKind > 1)
  300.             doCloseDoc (theWindow);
  301.             
  302.     ExitToShell ();
  303. }
  304. /* ===============================  EOF  =======================================
  305.     Copyright © 1990 by Code of the West, Inc. All Rights Reserved.
  306. ================================================================================ */